/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import domain.Client;
import domain.TableModel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;
//import mapping.PlayerMap;
import network.Network;

/**
 *
 * @author Jasper
 */
public class OverviewController {
    //private DatagramSocket datagramSocket;
    //private DatagramSocket dgSocket;
    private Client client;
    
    public OverviewController(Client client) {
        //try {
            //this.dgSocket = new DatagramSocket(Network.PORT, InetAddress.getLocalHost());
            this.client = client;
        //} catch (UnknownHostException ex) {
        //    Logger.getLogger(OverviewController.class.getName()).log(Level.SEVERE, null, ex);
        //} catch(SocketException e){
        //    e.printStackTrace();
        //}
    }
    
    //(UDP geloof ik, waarbij je dan ook een observer pattern implementeerd)
    private List<Client> getClients(String type) {
        List<Client> clients = new ArrayList<Client>();
        Socket link = null;
        try{
            link = new Socket(InetAddress.getLocalHost(), Network.PORT);
            PrintWriter output = new PrintWriter(link.getOutputStream(),true);
            
            //OPDRACHT --> SERVER
            output.println("getClients");
            output.flush();
            
            //TYPE --> SERVER
            output.println(type);
            
            InputStream inputStream = link.getInputStream();
            ObjectInputStream objectStream = new ObjectInputStream(inputStream);
            objectStream.close();
            
            clients = (ArrayList<Client>)objectStream.readObject();
        }catch(Exception e){
            e.printStackTrace();
        }
        return clients;
    }
    
    public TableModel getPlayerTableModel(String type){
        System.out.println(new TableModel(getClients(type)));
        return new TableModel(getClients(type));
    }
    
    //(TCP of UDP?)
    public void joinGame(String type){
        //server vraagt naar het type        
        //client geeft het type mee
        Socket link = null;
        try{
            //type --> server
            //server zorgt voor de background --> bij dat aangemaakte game zorgt hij dat de user tekenaar/rader wordt
            //daarna naar het drawpanel 
            link = new Socket(InetAddress.getLocalHost(), Network.PORT);
            PrintWriter output = new PrintWriter(link.getOutputStream());
            
            //OPDRACHT --> SERVER
            output.println("startGame");
            output.flush();
            
            //TYPE --> SERVER
            output.println(type);
            output.flush();
            
            
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
}
